package com.joker.aigc.chat.service.adapter;

import com.joker.aigc.chat.domain.dto.MessageDto;
import com.joker.aigc.chat.domain.entity.Message;
import com.joker.aigc.chat.domain.enums.MessageStatusEnum;

/**
 * Description: 消息适配器
 * Author: <a href="https://github.com/zongzibinbin">abin</a>
 * Date: 2023-03-26
 */
public class MessageAdapter {
    public static final int CAN_CALLBACK_GAP_COUNT = 100;

    public static Message buildMsgSave(MessageDto request, Long uid) {

        return Message.builder()
                .fromUid(uid)
                .roomId(request.getRoomId())
                .type(request.getMsgType())
                .status(MessageStatusEnum.NORMAL.getStatus())
                .build();
    }

//    public static List<MessageVo> buildMsgResp(List<Message> messages, List<MessageMark> msgMark, Long receiveUid) {
//        Map<Long, List<MessageMark>> markMap = msgMark.stream().collect(Collectors.groupingBy(MessageMark::getMsgId));
//        return messages.stream().map(a -> {
//                    MessageVo resp = new MessageVo();
//                    resp.setFromUser(buildFromUser(a.getFromUid()));
//                    resp.setMessage(buildMessage(a, markMap.getOrDefault(a.getId(), new ArrayList<>()), receiveUid));
//                    return resp;
//                })
//                .sorted(Comparator.comparing(a -> a.getMessage().getSendTime()))//帮前端排好序，更方便它展示
//                .collect(Collectors.toList());
//    }
//
//    private static MessageVo.Message buildMessage(Message message, List<MessageMark> marks, Long receiveUid) {
//        MessageVo.Message messageVO = new ChatMessageResp.Message();
//        BeanUtil.copyProperties(message, messageVO);
//        messageVO.setSendTime(message.getCreateTime());
//        AbstractMsgHandler<?> msgHandler = MsgHandlerFactory.getStrategyNoNull(message.getType());
//        if (Objects.nonNull(msgHandler)) {
//            messageVO.setBody(msgHandler.showMsg(message));
//        }
//        //消息标记
//        messageVO.setMessageMark(buildMsgMark(marks, receiveUid));
//        return messageVO;
//    }
//
//    private static MessageVo.MessageMark buildMsgMark(List<MessageMark> marks, Long receiveUid) {
//        Map<Integer, List<MessageMark>> typeMap = marks.stream().collect(Collectors.groupingBy(MessageMark::getType));
//        List<MessageMark> likeMarks = typeMap.getOrDefault(MessageMarkTypeEnum.LIKE.getType(), new ArrayList<>());
//        List<MessageMark> dislikeMarks = typeMap.getOrDefault(MessageMarkTypeEnum.DISLIKE.getType(), new ArrayList<>());
//        ChatMessageResp.MessageMark mark = new ChatMessageResp.MessageMark();
//        mark.setLikeCount(likeMarks.size());
//        mark.setUserLike(Optional.ofNullable(receiveUid).filter(uid -> likeMarks.stream().anyMatch(a -> Objects.equals(a.getUid(), uid))).map(a -> YesOrNoEnum.YES.getStatus()).orElse(YesOrNoEnum.NO.getStatus()));
//        mark.setDislikeCount(dislikeMarks.size());
//        mark.setUserDislike(Optional.ofNullable(receiveUid).filter(uid -> dislikeMarks.stream().anyMatch(a -> Objects.equals(a.getUid(), uid))).map(a -> YesOrNoEnum.YES.getStatus()).orElse(YesOrNoEnum.NO.getStatus()));
//        return mark;
//    }
//
//    private static MessageVo.UserInfo buildFromUser(Long fromUid) {
//        MessageVo.UserInfo userInfo = new MessageVo.UserInfo();
//        userInfo.setUid(fromUid);
//        return userInfo;
//    }

//    public static MessageVo buildAgreeMsg(Long roomId) {
//        MessageVo chatMessageReq = new MessageVo();
//        chatMessageReq.setRoomId(roomId);
//        chatMessageReq.setMsgType(MessageTypeEnum.TEXT.getType());
//        TextMsgReq textMsgReq = new TextMsgReq();
//        textMsgReq.setContent("我们已经成为好友了，开始聊天吧");
//        chatMessageReq.setBody(textMsgReq);
//        return chatMessageReq;
//    }
}
